<template>
  <div class="mod-config">
    <basic-container>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="请选择停车场" prop="parkingId">
          <el-select v-model="queryParams.parkingId" class="m-2" placeholder="请选择停车场">
            <el-option v-for="item in parkName" :key="item.id" :label="item.parkName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="请填写商家名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请填写商家名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-button icon="el-icon-plus" type="primary" @click="addHandle()">新增</el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border style="width: 100%">
          <el-table-column prop="parkName" header-align="center" align="center" label="所属停车场">
          </el-table-column>
          <el-table-column prop="name" header-align="center" align="center" label="商家名称">
          </el-table-column>
          <el-table-column prop="address" header-align="center" align="center" label="商家地址">
          </el-table-column>
          <el-table-column prop="loginName" header-align="center" align="center" label="登入账号">
          </el-table-column>
          <el-table-column prop="contacts" header-align="center" align="center" label="联系人">
          </el-table-column>
          <el-table-column prop="telephone" header-align="center" align="center" label="联系电话">
          </el-table-column>
          <el-table-column prop="merchantDescribe" header-align="center" align="center" label="商家描述">
          </el-table-column>
          <el-table-column prop="balance" header-align="center" align="center" label="余额">
          </el-table-column>
          <el-table-column prop="discount" header-align="center" align="center" label="折扣">
          </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" label="操作" width="200">
            <template #default="scope">
              <el-button link size="small" type="primary" icon="el-icon-edit" @click="addHandle(scope.row.id)">
                修改</el-button>
              <el-button link size="small" type="primary" icon="el-icon-delete" @click="delType(scope.row.id)">
                删除</el-button>
              <el-dropdown @command="handleCommand" trigger="click">
                <span class="el-dropdown-link">
                  <el-icon class="el-icon--right">
                    <arrow-down />
                  </el-icon> 更多
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item :command="handleCommandParams('a',scope.row.id)">充值</el-dropdown-item>
                    <el-dropdown-item :command="handleCommandParams('b',scope.row.id)">充值记录</el-dropdown-item>
                    <el-dropdown-item :command="handleCommandParams('c',scope.row.id)">余额变动记录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
          :current-page="queryParams.current" :page-sizes="[10, 20, 50, 100]" :page-size="queryParams.size"
          :total="total" background layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <tableForm v-if="merchantDialog" ref="merchantDia" @refreshDataList="getDataList" :parkName="parkName">
      </tableForm>
      <recharge v-if="recharge" ref="recharge" @refreshDataList="getDataList"></recharge>
    </basic-container>
  </div>
</template>

<script>
import { merchantList, delObj, } from '@/api/operate/merchant.js'
import { fetchList } from '@/api/roadStop/regionroad'
import tableForm from './components/tableForm.vue';
import recharge from './components/recharge.vue'
export default {
  name: 'merchant',
  components: {
    tableForm, recharge
  },
  data () {
    return {
      queryParams: {
        current: 1,
        size: 10
      },
      dataForm: {},
      parkName: [],
      dataList: [],
      total: 0,
      merchantDialog: false,
      recharge: false
    }
  },
  created () {
    this.getDataList()
    this.getDataListTwo()
  },
  methods: {
    handleCommandParams (command, merchantId) {
      //   console.log(command, parkingId);
      return {
        'command': command,
        'merchantId': merchantId
      }
    },
    handleCommand (item) {
      //   console.log(item)
      switch (item.command) {
        case 'a':
          this.recharge = true
          this.$nextTick(() => {
            this.$refs.recharge.init(item.merchantId)
          })
          break;
        case 'b':
          this.$router.push({
            path: "/operate/merchantrechargelog/index",
            query: {
              merchantId: item.merchantId
            },
          });
          break;
        case 'c':
          this.$router.push({
            path: "/operate/merchantchangelog/index",
            query: {
              merchantId: item.merchantId
            },
          });
          break;
      }
    },
    getDataListTwo () {
      fetchList({ isRoad: 1 }).then(response => {
        this.parkName = response.data.data
      })
    },
    getDataList () {
      merchantList(Object.assign(this.queryParams)).then(response => {
        this.dataList = response.data.data.records
        this.total = response.data.data.total
      })
    },
    addHandle (id) {
      this.merchantDialog = true
      this.$nextTick(() => {
        this.$refs.merchantDia.init(id)
      })
    },

    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    delType (id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      }).catch(() => { })
    },
    handleQuery () {
      this.queryParams.current = 1;
      this.getDataList()
    },
    resetQuery () {
      this.queryParams = {
        current: 1,
        size: 10,
      };
      this.handleQuery()
    }
  },
}
</script>

<style scope>
.el-dropdown-link {
  color: rgb(128, 128, 241);
  font-size: 12px;
  margin-top: 13%;
  cursor: pointer;
}
</style>